<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2016/12/21
 * Time: 11:24
 */

//生成器类 ¶

//Generator implements Iterator {
//	/* 方法 */
//	public mixed current ( void )
//	public mixed key ( void )
//	public void next ( void )
//	public void rewind ( void )
//	public mixed send ( mixed $value )
//	public void throw ( Exception $exception )
//	public bool valid ( void )
//	public void __wakeup ( void )
//}

function testYield($n){
	testYield($n--);
	if($n==0){
		return;
	}
	yield $n;
}
testYield(10);

function fib($n)
{
	$cur = 1;
	$prev = 0;
	for ($i = 0; $i < $n; $i++) {
		yield $cur;

		$temp = $cur;
		$cur = $prev + $cur;
		$prev = $temp;
	}
}

$fib = fib(10);
$fib->rewind();
while ($fib->valid()){
	echo $fib->current().PHP_EOL;
	$fib->next();
}